package com.storlead.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.storlead.security.model.RespBean;
import com.storlead.security.model.SysUser;
import com.storlead.security.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * @Author: Timo
 * @Date: 2019/3/15 12:04
 * @Description: 类用途：
 */
@RestController
public class LoginController {

    @Resource
    private SysUserService sysUserService;

//    @Autowired
//    private AuthenticationManager authenticationManager;

    @GetMapping("/submit_login")
    public JSONObject login(String username, String password) {
        JSONObject result = new JSONObject();
        username = Optional.ofNullable(username).orElse("123");
        password = Optional.ofNullable(password).orElse("123");

        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
//        Authentication authenticate = authenticationManager.authenticate(authRequest);
//        SecurityContextHolder.getContext().setAuthentication(authenticate);
//
//        HttpSession session = request.getSession();
//        session.setAttribute("SPRING_SECURITY_CONTEXT",SecurityContextHolder.getContext());

        result.put("error", "请先登录");
        return result;
    }

    @RequestMapping("/to_login")
    public RespBean toLogin(){
        return RespBean.error("Log in first, please");
    }


    @RequestMapping("/get")
    public RespBean get(){
        return RespBean.ok("this is get ");
    }

    @RequestMapping("/index")
    public RespBean index(){
        return RespBean.ok("this is  index");
    }

    @RequestMapping("/detail")
    public RespBean detail(){
        return RespBean.ok("this is detail");
    }

    @RequestMapping("/test")
    public RespBean test(){
        return RespBean.ok("this is test ");
    }
}
